package com.jie.eclipse.pan.config;

import java.util.ArrayList;
import java.util.List;

import com.jie.eclipse.pan.config.dp.SearchPanelConfig;
import com.jie.eclipse.pan.config.table.TableConfig;
import com.jie.eclipse.pan.config.toolbar.ToolbarConfig;
import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.popup.wizard.BaseWizard;
import com.jie.eclipse.pan.utils.StringHelper;
import com.jie.eclipse.pan.xml.XMLNode;

public class BasePageConfig extends BaseConfig{
	
	protected BaseWizard baseWizard = null;
	private final static String TAG_NODENAME = "PAGE";
	public final static String TAG_TPTYPE_BASE_GRID = "base_grid";
	public final static String TAG_TPTYPE_BASE_FORM = "base_form";
	public final static String TAG_TPTYPE_BASE_LOADING = "base_loading";
	public final static String TAG_TPTYPE_BASE_PRINT = "base_print";
	public final static String TAG_TPTYPE_BASE_REPORT = "base_report";
	public final static String TAG_TPTYPE_ADVANCED_GRID1 = "advanced_grid1";//左查右编
	public final static String TAG_TPTYPE_ADVANCED_GRID2 = "advanced_grid2";//表格
	
	private String tpType = "";//模板类型
	private String pageName = "";//页面名称
	private String pageTitle = "";//页面标题
	private ToolbarConfig tbConfig = null;//工具栏配置
	private SearchPanelConfig spConfig = null;//查询面板
	private List<TableConfig> tableList = null;
	
	public BasePageConfig(){
		this.spsv(BaseConfig.TAG_LABLE, "页面");
	}
	
	@Override
	public void OnSetProperty(String key, Object objValue){
		this.getDataRecord().spov(key, objValue);
		if("tpType".equalsIgnoreCase(key)){
			setTpType(StringHelper.getValue(objValue));
		}else if("pageName".equalsIgnoreCase(key)){
			setPageName(StringHelper.getValue(objValue));
		}else if("pageTitle".equalsIgnoreCase(key)){
			setPageTitle(StringHelper.getValue(objValue)); 
		}else if("tbConfig".equalsIgnoreCase(key)){
			tbConfig = new ToolbarConfig();
			tbConfig.init();
			tbConfig.setPageConfig(this);
			tbConfig.loadConfig((BaseDataEntity)objValue);
		}else{
			super.OnSetProperty(key, objValue);
		}
	}
	
	@Override
	public void Reset() {
		super.Reset();
		this.pageName = "";
		this.tpType = "";
		this.pageTitle = "";
	}
	
	
	@Override
	public void FillXmlNode(XMLNode page) {
		super.FillXmlNode(page);
		page.setNodeName("Page");
		page.SetValue("tpType", this.getTpType());
		page.SetValue("pageName", this.getPageName());
		page.SetValue("pageTitle", this.getPageTitle());
		
		//工具栏
		XMLNode toolbarNode = new XMLNode();
		this.getTbConfig().FillXmlNode(toolbarNode);
		page.AddNode(toolbarNode);
		
		if("grid".equalsIgnoreCase(this.getTpType())){
			//表格模板
			
			//1.查询条件
			if(spConfig == null){//设置默认查询面板
				spConfig = new SearchPanelConfig(this);
				spConfig.init();
			}
			
			XMLNode spNode = new XMLNode();
			this.spConfig.FillXmlNode(spNode);
			page.AddNode(spNode);
			
			//2.列表部件 & 表格
			XMLNode listNode = new XMLNode();
			listNode.setNodeName("tableList");
			listNode.spsv(TAG_LABLE, "列表部件");
			page.AddNode(listNode);
			
			//表格
			if(this.tableList == null){//设置默认表格
				TableConfig tableConfig = new TableConfig(this);
				tableConfig.init();
				tableConfig.spsv("id", "grid1");
				this.addTable(tableConfig);
			}
			
			for (int i = 0; i < this.tableList.size(); i++) {
				TableConfig tableConfig = this.tableList.get(i);
				XMLNode tableNode = new XMLNode();
				tableConfig.FillXmlNode(tableNode);
				listNode.AddNode(tableNode);
			}
			
			
		}
	}
	
	/**
	 * @return 返回有几张表格
	 */
	public int getTableSize(){
		if(this.tableList == null)return 0;
		return this.tableList.size();
	}
	
	/**
	 * @param tableConfig
	 * 新增表格
	 */
	public void addTable(TableConfig tableConfig){
		if(this.tableList == null)
			this.tableList = new ArrayList<TableConfig>();
		this.tableList.add(tableConfig);
	}
	
	/**
	 * @return
	 * 工具栏config
	 */
	public ToolbarConfig getTbConfig() {
		return tbConfig;
	}
	public void setTbConfig(ToolbarConfig tbConfig) {
		this.tbConfig = tbConfig;
	}
	
	/**
	 * @return 查询面板
	 */
	public SearchPanelConfig getSpConfig() {
		return spConfig;
	}

	public void setSpConfig(SearchPanelConfig spConfig) {
		this.spConfig = spConfig;
	}
	
	public String getTpType() {
		return tpType;
	}
	public void setTpType(String tpType) {
		this.tpType = tpType;
	}
	public String getPageName() {
		return pageName;
	}
	public void setPageName(String pageName) {
		this.pageName = pageName;
	}
	public String getPageTitle() {
		return pageTitle;
	}
	public void setPageTitle(String pageTitle) {
		this.pageTitle = pageTitle;
	}
	
	public BaseWizard getBaseWizard() {
		return baseWizard;
	}

	public void setBaseWizard(BaseWizard baseWizard) {
		this.baseWizard = baseWizard;
	}
	
	@Override
	protected Object CreateCloneObject() {
		return new BasePageConfig();
	}
}
